<?php

function comments($module, $whatid, $theme_='comments_main.php') {
	global $conf, $thm_img;
	lang_include('comments');

	if(!perms_check('comments', 'read'))
		return false;

	$db = new dbquery;
	$db2 = new dbquery;

	SQLvalidate($_GET['rec_c']);
	SQLvalidate($whatid, 'int', 10);
	SQLvalidate($module, 'varchar', 60);

	if(!in_array($module, $conf['comments_tables'])) {
		redirect($_SESSION['redirect_2']);
		exit;
	}

	if(!is_numeric($_GET['rec_c']))
		$_GET['rec_c']=0;

	//selecting records from comments table
	$db->query("SELECT * FROM $conf[prefix]comments WHERE what='$module' && whatid=$whatid && lang='".$_SESSION['lang_short']."' && visible=1") or $db->err(__FILE__, __LINE__);
	$q=$db->num_rows();
	$db->query("SELECT * FROM $conf[prefix]comments WHERE what='$module' && whatid=$whatid && lang='".$_SESSION['lang_short']."' && visible=1 ORDER BY id DESC LIMIT $_GET[rec_c], $conf[comments_per_page]") or $db->err(__FILE__, __LINE__);
	//

	//generating comments
	while($d=@$db->fetch_object()) {
		$pm='';
		$info='';
		$gg='';
		$o2='';
		$icq='';
		$www='';
		$quote='';
		$quote_selection='';
		$mail='';
		$signature='';
		$edit='';
		$delete='';
		$GLOBALS['buttons']='';
		
		//sprawdzanie czy komentujacy jest zarejestrowany, wybranie avataru {image}
		if(!is_numeric($d->who)) {
			$login_=$d->who;
			$image='';
			if($d->mail) {
				$mail=explode("@", $d->mail);
				$mail='<span class="btn"><a href="javascript:mail(\''.$mail[0].'\', \''.$mail[1].'\');"><img src="'.$GLOBALS['theme_path'].$GLOBALS['theme_img']['mail'].'" alt="" /></a></span>';
			}
		}
		else {
			$GLOBALS['user']=get_user_by_id($d->who);
			$login_=$GLOBALS['user']->login;

			plugins('users/themes/users_buttons/');

			//signature
			if($GLOBALS['user']->signature)
				$signature='____<br />'.read_text_comments($GLOBALS['user']->signature);
			//

			if($GLOBALS['user']->avatar=='none')
				$image='';
			elseif(ereg('http:\/\/', $GLOBALS['user']->avatar))
				$image='<img src="'.$GLOBALS['user']->avatar.'" alt="'.$login_.'" />';
			else
				$image='<img src="'.$conf['avatar_upload'].$GLOBALS['user']->avatar.'" alt="'.$login_.'" />';
		}

		//quote
		$quote_text='[quote='.$login_.']'.overlib_text($d->text).'[/quote]';
		$quote='<span class="btn"><a href="javascript:add_content(\''.$quote_text.'\',document.cmts_form.text);" target="_parent" title="'.$GLOBALS['lang']['global']['quote'].'"><img src="'.$GLOBALS['theme_path'].$GLOBALS['theme_img']['quote'].'" alt="'.$GLOBALS['lang']['global']['quote'].'" /></a></span>';
		$quote_selection='<span class="btn"><a href="javascript:void(null)" onmousedown="quoteSelection(\''.$login_.'\',document.cmts_form.text);" title="'.$GLOBALS['lang']['global']['quote_selection'].'"><img src="'.$GLOBALS['theme_path'].$GLOBALS['theme_img']['quote_selection'].'" alt="'.$GLOBALS['lang']['global']['quote_selection'].'" /></a></span>';
		//

		//adding {edit} & {delete} if user is admin or author of this comment
		if(perms_check('comments', 'edit'))
			$edit='<span class="btn"><a href="index.php?module=admin&amp;action=comments&amp;cmd=edit&amp;id='.$d->id.'" title="'.$GLOBALS['lang']['global']['edit'].'">'.$GLOBALS['lang']['global']['edit'].'</a></span>';
		elseif($d->who==$_SESSION['id'])
			$edit='<span class="btn"><a href="index.php?module=comments&amp;cmd=edit&amp;id='.$d->id.'" title="'.$GLOBALS['lang']['global']['edit'].'">'.$GLOBALS['lang']['global']['edit'].'</a></span>';

		if(perms_check('comments', 'del') or $d->who==$_SESSION['id'])
			$delete='<span class="btn"><a href="index.php?module=comments&amp;cmd=really_delete&amp;id='.$d->id.'" title="'.$GLOBALS['lang']['global']['delete'].'">'.$GLOBALS['lang']['global']['delete'].'</a></span>';
		//

		$d->text=read_text_comments($d->text);
		$array=array('date' => date($conf['date_format'], strtotime($d->date)), 'title' => read_text_rest($d->title), 'text' => $d->text, 'theme_path' => $GLOBALS['theme_path'], 'image'=>$image, 'edit'=>$edit, 'delete'=>$delete, 'quote'=>$quote, 'quote_selection'=>$quote_selection, 'buttons'=>$GLOBALS['buttons'], 'login'=>$GLOBALS['user']->login, 'signature'=>$signature);

		$content_.=string_template(read_file('comments/comments_item.php'), $array);
	}
	//

	$url=str_replace("&comments=1", '', $_SERVER['REQUEST_URI']);
	$url=eregi_replace("\&rec_c\=[0-9]+", '', $url);

	//page_link
	$page_link=split_to_pages('<a href="'.$url.'&amp;rec_c={rec}#cmts">{nr}</a>', $conf['comments_per_page'], $q, $_GET['rec_c']);
	//

	//comment_form
	if(perms_check('comments', 'write')) {
		$author=get_user_by_id($_SESSION['id']);
		$comments_form=string_template(read_file('comments/comment_form.php'), array('theme_path'=>$GLOBALS['theme_path'], 'error'=>$lang[$_GET['error']], 'redirect'=> end($foo = explode('/',$_SERVER['REQUEST_URI'])), 'what'=>$module, 'whatid'=>$whatid, 'function'=>'comment_add'));
	}
	//

	//wrzucenie do comments_main komentarzy
	return string_template(read_file('comments/'.$theme_), array('theme_path'=>$GLOBALS['theme_path'], 'comments_form'=>$comments_form, 'content'=>$content_, 'page_link'=>$page_link));
	//
}

//adding ability to comment $module, need cat column in $prefix_$module table
function comments_fields_add($module) {
	global $conf;
	
	if(!perms_check('admin', 'modules'))
		exit;
	
	$conf['comments_tables'][]=$module;
	
	$db=new dbquery;
	$db->query("UPDATE $conf[prefix]conf SET value='[array]".implode('=>', $conf['comments_tables'])."[/array]' WHERE name='comments_tables'") or $db->err(__FILE__, __LINE__);
	$db->clear_cache('conf*', '');
}

//disabling ability to comment $module, need cat column in $prefix_$module table
function comments_fields_delete($module) {
	global $conf;
	
	if(!perms_check('admin', 'modules'))
		exit;
	
	$ct=array();
	foreach ($conf['comments_tables'] as $i) {
		if($i!=$module)	$ct[]=$i;
	}
	
	$db=new dbquery;
	$db->query("UPDATE $conf[prefix]conf SET value='[array]".implode('=>', $ct)."[/array]' WHERE name='comments_tables'") or $db->err(__FILE__, __LINE__);
	$db->clear_cache('conf*', '');
}

function update_tpl_cache($what, $whatid) { // funkcja usuwajaca cache templatow 
	$tpl = new tpl;
	$tpl->clear_cache('*module-'.$what.'_id-'.$whatid.'*');
}
?>
